<?php
include_once 'paperblog.php';

if (!isIpAllow()) {
    exit('ip deny');
}
if (!session_start()) {
    exit('session start failed');
}
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
    unset($_SESSION['user']);
}
if (isset($_POST['action']) && $_POST['action'] === '登录' && isset($_POST['user']) && isset($_POST['password'])) {
    $users = include 'user.php';
    foreach ($users as $user) {
        if ($user[0] === $_POST['user'] && md5($_POST['password']) === $user[1]) {
            $_SESSION['user'] = $user[0];
            break;
        }
    }
}
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Paper Blog 管理后台</title>
    <style>
        ul.menus li {
            margin: 5px;
        }
        body {
            font-family: Verdana,Arial,Helvetica,sans-serif;
        }
        h1 {
            font-size: 16px;
            font-weight: bold;
            color: #274571;
            margin: 0;
        }
        form {
            width: 305px;
            /*margin: 0 auto;*/
            border: 1px solid #ACC3E4;
            border-radius: 5px 5px 5px 5px;
            box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.15);
            background: #EDF5FF;
            padding: 20px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -172px;
            margin-top: -115px;
        }
        form div {
            padding: 5px 0;
            position: relative;
        }

        form input[type=text], form input[type=password] {
            -moz-transition: box-shadow 0.1s linear 0s;
            background: none repeat scroll 0 0 transparent;
            border: 1px solid #9DADC5;
            border-radius: 3px 3px 3px 3px;
            color: #494949;
            font-size: 20px;
            height: 34px;
            line-height: 34px;
            outline: medium none;
            padding: 0 4px;
            position: relative;
            width: 295px;
        }
        form input[type=text]:focus, form input[type=password]:focus {
            border: 1px solid #9BBBDA;
            box-shadow: 0 0 5px rgba(110, 189, 222, 0.5);
        }
        form .submit-wrapper {
            -moz-border-bottom-colors: none;
            -moz-border-image: none;
            -moz-border-left-colors: none;
            -moz-border-right-colors: none;
            -moz-border-top-colors: none;
            -moz-transition: box-shadow 0.1s linear 0s;
            background: url("resources/images/login091531.png") repeat-x scroll 0 0 #67A00D;
            border-color: #6B9F11 #54850A #386601;
            border-radius: 3px 3px 3px 3px;
            border-style: solid;
            border-width: 1px;
            display: inline-block;
            height: 30px;
            margin-top: 9px;
            width: 82px;
        }

        form .submit-wrapper:hover {
            background-position: 0 -32px;
            box-shadow: 0 0 3px rgba(84, 133, 10, 0.5);
            text-decoration: none;
        }

        form input[type=submit] {
            background: none repeat scroll 0 0 transparent;
            border: medium none;
            color: #FFFFFF;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            height: 30px;
            line-height: 25px;
            width: 82px;
        }
        form label {
            background: url("resources/images/login091531.png") no-repeat scroll -1px -97px white;
            border-radius: 3px 3px 3px 3px;
            color: #8B9096;
            font-size: 14px;
            height: 34px;
            left: 1px;
            line-height: 34px;
            padding: 0 7px;
            position: absolute;
            top: 6px;
            width: 289px;
        }
    </style>
    <script>
        var onReady = function () {
            var inputUser = document.getElementById('user');
            var inputPass = document.getElementById('password');
            var onFocus = function (e) {
                if (e.target.value == '') {
                    e.target.parentNode.getElementsByTagName('label')[0].style.color = 'rgb(204, 204, 204)';
                }
            };
            var onBlur = function (e) {
                if (e.target.value == '') {
                    e.target.parentNode.getElementsByTagName('label')[0].style.color = '#8B9096';
                }
            };
            var onKeyDown = function (e) {
                e.target.parentNode.getElementsByTagName('label')[0].style.color = '#FFFFFF';
            };
            var onKeyUp = function (e) {
                if (e.target.value) {
                    e.target.parentNode.getElementsByTagName('label')[0].style.color = '#FFFFFF';
                } else {
                    e.target.parentNode.getElementsByTagName('label')[0].style.color = 'rgb(204, 204, 204)';
                }
            };
            inputUser.addEventListener('focus', onFocus, false);
            inputUser.addEventListener('blur', onBlur, false);
            inputPass.addEventListener('focus', onFocus, false);
            inputPass.addEventListener('blur', onBlur, false);
            inputUser.addEventListener('keydown', onKeyDown, false);
            inputUser.addEventListener('keyup', onKeyUp, false);
            inputPass.addEventListener('keydown', onKeyDown, false);
            inputPass.addEventListener('keyup', onKeyUp, false);
        };
        document.addEventListener('DOMContentLoaded', onReady, false);
    </script>
</head>
<body>
    <?php
    if (isset($_SESSION['user'])) {
    ?>
    <div>当前用户： <?php echo $_SESSION['user'] ?> <a href="index.php?action=logout">退出</a></div>
    <nav>
        <ul class="menus">
            <li><a href="add.php">写博客</a></li>
            <li><a href="posts.php">文章列表</a></li>
            <li><a href="nav.php">导航管理</a></li>
            <li><a href="link.php">友情链接</a></li>
            <li><a href="category.php">分类管理</a></li>
            <li><a href="tag.php">标签管理</a></li>
            <li><a href="setting.php">更改配置</a></li>
            <li><a href="compile.index.php">重新生成首页</a></li>
            <li><a href="compile.post.php">重新生成文章页面</a></li>
            <li><a href="compile.category.php">重新生成分类页面</a></li>
            <li><a href="compile.tag.php">重新生成标签页面</a></li>
            <li><a href="setting.php">重置数据</a></li>
        </ul>
    </nav>
    <?php
    } else {
    ?>
    <form action="index.php" method="POST">
        <h1>Paper Blog 管理后台</h1>
        <div>
            <label for="user">用户名</label><input type="text" name="user" id="user" />
        </div>
        <div>
            <label for="password">密码</label><input type="password" name="password" id="password" />
        </div>
        <div>
            <a class="submit-wrapper">
                <input type="submit" name="action" value="登录" />
            </a>
        </div>
    </form>
    <?php
    }
    ?>
</body>
</html>
